// =======================================================================
//  File: EntityGenerator.cs
// =======================================================================
//  This file is part of Addon Nice product.
//  Copyright (C) Addon Nice.  All rights reserved.
// =======================================================================

/* $RCSFile: EntityGenerator.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/CrmGenClass/EntityGenerator.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:49  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:24  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using CrmSdk;
using System.Collections.Generic;
using AddonNice.Settings;
using System.Reflection;

namespace AddonNice.CRMWrapper
{
    /// <summary>
    /// This class allows to generate an Addon Nice C# class for a crm entity.
    /// </summary>
    public class EntityGenerator: EntityConfig
    {
        public string lowEntityName;
        public string className;

        /// <summary>
        /// Version where flags default to EntityFlags.All 
        /// </summary>
        public EntityGenerator(CRMWrapperBase w, string entName) : base(w, entName)
        {
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( 
                    string.Format("[V]EntityGenerator Ctor Entity:{0}.", entName),CRMQueryTraceSwitch.Sw.Info);
        }


        /// <summary>
        /// Create a DataTable with all columns from the Crm entity
        /// </summary>
        public string CreateClass()
        {
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EntityConfig CreateClass BEG attributesDic.Keys.Count: {0}",  
                    attributesDic.Keys.Count),CRMQueryTraceSwitch.Sw.Info);
            StringBuilder sb    =   new StringBuilder();
            lowEntityName       =   entityName.ToLower();
            string ent          =   char.ToUpper(lowEntityName[0])+lowEntityName.Substring(1);
            className           =   string.Format("ADNDyn{0}", ent);
            sb.Append("/* Copyrights Christian Surieux 2010 */\r\n");
            sb.AppendFormat("/* Class {0} */\r\n",className);
            sb.AppendFormat("/* Date {0:u} */\r\n\r\n",DateTime.Now);
            // usings+namespace+class
            sb.Append("using System;\r\nusing System.Diagnostics;\r\nusing System.Collections;\r\nusing System.ComponentModel;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing CrmSdk;\r\n\r\n");
            // open namespace
            sb.Append("namespace AddonNice.CRMWrapper\r\n{\r\n");
            StringBuilder sb1    =   new StringBuilder();
            // const vars def
            sb1.Append("\tpublic partial class CrmStr\r\n\t{\r\n");
            Type CrmStrTp       =   typeof(CrmStr);
            FieldInfo[] StrCst  =   CrmStrTp.GetFields();
            bool shouldWrite    =   false;
            List<string> constLst   =   new List<string>(attributesDic.Keys);
            constLst.Insert(0,lowEntityName); // add the entity name
            foreach (string key in constLst)
            {
                switch ( key )
                {
                    case CrmStr.createdby:
                    case CrmStr.createdbyname:
                    case CrmStr.createdbyyominame:
                    case CrmStr.modifiedby:
                    case CrmStr.modifiedbyname:
                    case CrmStr.modifiedbyyominame:
                    case CrmStr.createdon:
                    case CrmStr.overriddencreatedon:
                    case CrmStr.modifiedon:
                    case CrmStr.ownerid:
                    case CrmStr.owneridname:
                    case CrmStr.owneridtype:
                    case CrmStr.owneridyominame:
                    case CrmStr.statecode:
                    case CrmStr.statecodename:
                    case CrmStr.statuscode:
                    case CrmStr.statuscodename:
                    case CrmStr.importsequencenumber:
                    case CrmStr.versionnumber:
                    continue;
                }
                if ( key.EndsWith("dsc") )
                    continue;
                var  fldLst          =   StrCst.Where(s => (s.Name == key));
                if ( fldLst.Count() != 0 )
                {
                    foreach( FieldInfo s in fldLst)
                        sb1.AppendFormat("\t/* found {0}={1} */\r\n",key,s.Name);
                    continue;
                }
                sb1.AppendFormat("\t\tpublic const string {0}\t\t\t\t=\t\"{0}\";\r\n",key);
                shouldWrite         =   true;
            }
            sb1.Append("\t}\r\n\r\n");
            if ( shouldWrite )
                sb.Append(sb1.ToString());
            // Ctors
            CreateCtors(sb);

            // Properties
            foreach (string key in attributesDic.Keys)
            {
                switch ( key )
                {
                    case CrmStr.createdby:
                    case CrmStr.createdbyname:
                    case CrmStr.createdbyyominame:
                    case CrmStr.modifiedby:
                    case CrmStr.modifiedbyname:
                    case CrmStr.modifiedbyyominame:
                    case CrmStr.deletionstatecode:
                    case CrmStr.createdon:
                    case CrmStr.overriddencreatedon:
                    case CrmStr.modifiedon:
                    case CrmStr.ownerid:
                    case CrmStr.owneridname:
                    case CrmStr.owneridtype:
                    case CrmStr.owneridyominame:
                    case CrmStr.statecode:
                    case CrmStr.statecodename:
                    case CrmStr.statuscode:
                    case CrmStr.statuscodename:
                    case CrmStr.importsequencenumber:
                    case CrmStr.utcconversiontimezonecode:
                    case CrmStr.versionnumber:
                    continue;
                }
                if ( key.EndsWith("dsc") )
                    continue;
                if ( key.EndsWith("idname") )
                    continue;
                AddProperty(sb,key);
            }
            sb.Append("\t}\r\n}");// class + namespace
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EntityConfig CreateClass END sz: {0}",sb.Length),CRMQueryTraceSwitch.Sw.Info);
            return sb.ToString();
        }

        public void CreateCtors(StringBuilder sb)
        {
            bool hasPrimaryField    =   !string.IsNullOrEmpty(PrimaryField);
            bool hasPrimaryKey      =   !string.IsNullOrEmpty(PrimaryKey);

            sb.AppendFormat("\tpublic class {0} : DynEntity\r\n\t{{\r\n",className);
	        // The internal class for DynTypes
            sb.Append("\t\t/* Internal class for DynTypes */\r\n");
            sb.AppendFormat("\t\tprotected class Ftor: DFactor\r\n\t\t{{\r\n\t\t\tpublic Ftor() : base(CrmStr.{0}){{}}\r\n",lowEntityName);
	        sb.AppendFormat("\t\t\tpublic override Type DynType\r\n\t\t\t{{\tget{{return typeof({0});}}}}\r\n",className);
	        sb.AppendFormat("\t\t\tpublic override string DynTypename\r\n\t\t\t{{\tget{{return CrmStr.{0};}}}}\r\n",lowEntityName);
	        sb.AppendFormat("\t\t\tpublic override DynEntity Build()\r\n\t\t\t{{\treturn new {0}();}}\r\n",className);
	        sb.AppendFormat("\t\t\tpublic override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)\r\n\t\t\t{{\treturn new {0}(w, dEntity, wproperties, null);}}\r\n",className);
            if ( hasPrimaryField )
	             sb.AppendFormat("\t\t\tpublic override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)\r\n\t\t\t{{\treturn new {0}(w, PrimaryFieldValue);}}\r\n",className);
	        else sb.AppendFormat("\t\t\tpublic override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)\r\n\t\t\t{{\treturn new {0}(w);}}\r\n",className);
	        if ( hasPrimaryKey )
                 sb.AppendFormat("\t\t\tpublic override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)\r\n\t\t\t{{\treturn new {0}(w,PrimaryKeyValue);}}\r\n",className);
            else sb.AppendFormat("\t\t\tpublic override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)\r\n\t\t\t{{\treturn new {0}(w);}}\r\n",className);
            sb.Append("\t\t}\r\n\t\t/* Static Ctor to register in DynTypes */\r\n");
            sb.AppendFormat("\t\tstatic {0}(){{new Ftor();}}\r\n\r\n",className);
            // main properties
            if ( !attributesDic.ContainsKey(CrmStr.ownerid) )
                sb.Append("\t\tpublic override bool HasOwnerProperty\r\n\t\t{\tget{ return false;}}\r\n");
            if ( attributesDic.ContainsKey(CrmStr.businessunitid) )
                sb.Append("\t\tpublic override bool HasBusinessUnitProperty\r\n\t\t{\tget{ return true;}}\r\n");
            if ( attributesDic.ContainsKey(CrmStr.owningbusinessunit) )
                sb.Append("\t\tpublic override bool HasOwningBusinessUnitProperty\r\n\t\t{\tget{ return true;}}\r\n");
            if ( !hasPrimaryField )
            {
                sb.Append("\t\tpublic override bool HasPrimaryField\r\n\t\t{\tget\t{\treturn false;}\r\n\t}\r\n\r\n");
            }
            
            if ( !hasPrimaryKey )
            {
                sb.Append("\t\tpublic override bool HasPrimaryKey\r\n\t\t{\tget\t{\treturn false;}\r\n\t}\r\n\r\n");
            }

            // Ctors
            sb.AppendFormat("\t\tpublic {0}(): base(){{}}\r\n",className);
            sb.AppendFormat("\t\tpublic {0}(CRMWrapperBase w, string PrimaryFieldValue): base(w, CrmStr.{1})\r\n\t\t{{",className,lowEntityName);
            if ( hasPrimaryField )
                    sb.AppendFormat("\r\n\t\t\tif ( PrimaryFieldValue != string.Empty )\r\n\t\t\t{0}\t=   PrimaryFieldValue;\r\n\t\t}}\r\n",PrimaryField);
            else    sb.Append("\t\t}\r\n");
            sb.AppendFormat("\t\tpublic {0}(CRMWrapperBase w, Guid CrmId): base(w,CrmStr.{1}, CrmId){{}}\r\n",className,lowEntityName);

            sb.AppendFormat("\t\tpublic {0}(CRMWrapperBase w, InternalDynamicEntity e): base(w,CrmStr.{1},e,null,null){{}}\r\n",className,lowEntityName);

            sb.AppendFormat("\t\tpublic {0}(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(w,CrmStr.{1},e,prop,s){{}}\r\n",className,lowEntityName);

            sb.AppendFormat("\t\tpublic {0}(ResetWrapperHandler fn, string PrimaryFieldValue): base(fn,CrmStr.{1})\r\n\t\t{{",className,lowEntityName);
            if ( hasPrimaryField )
                    sb.AppendFormat("\r\n\t\t\tif ( PrimaryFieldValue != string.Empty )\r\n\t\t\t\t{0}\t=   PrimaryFieldValue;\r\n\t\t}}\r\n",PrimaryField);
            else    sb.Append("}\r\n");

            sb.AppendFormat("\t\tpublic {0}(ResetWrapperHandler fn, Guid CrmId): base(fn,CrmStr.{1},CrmId){{}}\r\n",className,lowEntityName);

            sb.AppendFormat("\t\tpublic {0}(ResetWrapperHandler fn,InternalDynamicEntity e): base(fn,CrmStr.{1},e,null,null){{}}\r\n",className,lowEntityName);

            sb.AppendFormat("\t\tpublic {0}(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(fn,CrmStr.{1},e,prop,s){{}}\r\n",className,lowEntityName);
            sb.AppendLine("\r\n\t\t/* Properties */\r\n");
            if ( hasPrimaryKey )
            {
                sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic Guid {1}\r\n\t\t{{\r\n\t\tget\t{{\t return KeyPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddKeyProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n",lowEntityName,PrimaryKey);
                sb.AppendFormat("\t\tprotected override void SetCrmIdProperty(Guid crmId)\r\n\t\t{{\r\n\t\t\tif ( !dicProperty.ContainsKey(CrmStr.{0})  || KeyPropertyValue(CrmStr.{0}) != crmId )\r\n\t\t\t\t{0}\t=   crmId;\r\n\t\t}}\r\n",PrimaryKey);

                sb.AppendFormat("\t\tpublic override Guid PrimaryKeyGuidValue\r\n\t\t{{\r\n\t\tget\t{{\treturn {0};}}\r\n\t\tset{{\t{0}\t=value;}}\r\n\t\t}}\r\n",PrimaryKey);
            }
            if ( hasPrimaryField )
            {
                sb.AppendFormat("\t\tpublic override string PrimaryFieldName\r\n\t\t{{\r\n\t\tget\t{{\treturn {0};}}\r\n\t\tset\t{{\tbase.PrimaryFieldName = value;}}\r\n\t\t}}\r\n\r\n",PrimaryField);
            }
        }

        void AddString(StringBuilder sb,string Key)
        {
            // overridden methods
            if ( ( Key == CrmStr.name ) )
                 sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic override string {1}\r\n\t\t{{\r\n\t\tget\t{{\t return StringPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddStringProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
            // read only
            else if ( Key == CrmStr.organizationid ) 
                 sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic override string {1}\r\n\t\t{{\r\n\t\tget\t{{\t return StringPropertyValue(CrmStr.{1});}}\r\n\t}}\r\n\r\n",lowEntityName,Key);
            else sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic string {1}\r\n\t\t{{\r\n\t\tget\t{{\t return StringPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddStringProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddNumber(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic int {1}\r\n\t\t{{\r\n\t\tget\t{{\t return NumberPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddNumberProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddFloat(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic double {1}\r\n\t\t{{\r\n\t\tget\t{{\t return DoublePropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddFloatProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddDecimal(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic decimal {1}\r\n\t\t{{\r\n\t\tget\t{{\t return DecimalPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddDecimalProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddMoney(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic decimal {1}\r\n\t\t{{\r\n\t\tget\t{{\t return MoneyPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddMoneyProperty(CrmStr.{1}, value);}}\r\n\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddBooleanProperty(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic bool {1}\r\n\t\t{{\r\n\t\t\tget\t{{\t return BooleanPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddBooleanProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddDateTime(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic string {1}\r\n\t\t{{\r\n\t\tget\t{{\t return DateTimePropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddDateTimeProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddUniqueIdentifier(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic Guid {1}\r\n\t\t{{\r\n\t\tget\t{{\t return UniqueIdentifierPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddUniqueIdentifierProperty(CrmStr.{1}, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        /// <summary>
        /// Default to account
        /// </summary>
        void AddCustomer(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic Guid {1}\r\n\t\t{{\r\n\t\tget\t{{\t return CustomerPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddCustomerProperty(CrmStr.{1},CrmStr.account,value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        void AddOwner(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t[CrmDynAttribute(CrmStr.{0})]\r\n\tpublic Guid {1}\r\n\t\t{{\r\n\t\tget\t{{\t return OwnerPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddOwnerProperty(CrmStr.{1},CrmStr.systemuser, value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
        }

        /// <summary>
        /// Try to deduct entit from lookup
        /// </summary>
        void AddLookup(StringBuilder sb,string Key)
        {
            string lk   =   "\"CrmStr.xxx\"";
            if ( Key.EndsWith("id") )
                lk      =   string.Format("CrmStr.{0}",Key.Substring(0,Key.Length-2));
            switch(Key)
            {
                case CrmStr.owningbusinessunit:
                    lk  =   "CrmStr.businessunit";
                break;
                case CrmStr.owninguser:
                    lk  =   "CrmStr.systemuser";
                break;
                case CrmStr.owningteam:
                    lk  =   "CrmStr.team";
                break;
                case "pricelistid"://CrmStr.pricelistid:
                    lk  =   "CrmStr.pricelevel";
                break;
            }
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic Guid {1}\r\n\t\t{{\r\n\t\tget\t{{\t return LookupPropertyValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddLookupProperty(CrmStr.{1},{2},value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key,lk);
            sb.AppendFormat("\t\tpublic string {1}Name\r\n\t\t{{\r\n\t\tget\t{{\t return LookupPropertyName(CrmStr.{0});}}\r\n\t\t}}\r\n\r\n",Key,lk.Replace("CrmStr.",string.Empty));
        }

        void AddPickList(StringBuilder sb,string Key)
        {
            sb.AppendFormat("\t\t[CrmDynAttribute(CrmStr.{0})]\r\n\t\tpublic int {1}\r\n\t\t{{\r\n\t\tget\t{{\t return PicklistPropertyIntValue(CrmStr.{1});}}\r\n\t\tset\t{{\tAddPicklistProperty(CrmStr.{1},value);}}\r\n\t\t}}\r\n\r\n",lowEntityName,Key);
            sb.AppendFormat("\t\tpublic string {0}Label\r\n\t\t{{\r\n\t\tget\t{{\t return PicklistPropertyLabelValue(CrmStr.{0});}}\r\n\t\t}}\r\n\r\n",Key);
        }


        public void AddProperty(StringBuilder sb, string key)
        {
            string Name                 =   AttFieldAsString(key,InternalAttributeFields.Name);
            string DisplayName          =   AttFieldAsString(key,InternalAttributeFields.DisplayName);
            string attType              =   AttFieldAsString(key,InternalAttributeFields.Type);
            int attTypeInt              =   AttFieldAsInt(key,InternalAttributeFields.Type);
            int DisplayMask             =   AttFieldAsInt(key,InternalAttributeFields.DisplayMask);
            AttributeInternalType atttp =   (AttributeInternalType )attTypeInt;
            if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EntityGenerator AddColumn BEG name: {0}, displayName: {1}, Type: {2}, IType: {3}",
                    new object[]{Name,DisplayName,attType,atttp} ),CRMQueryTraceSwitch.Sw.Info);
            if ( ( DisplayMask == (int)InternalDisplayMasks.ObjectTypeCode ) || 
                (Name == CrmStr.objecttypecode && atttp == AttributeInternalType.Picklist) )
            {
                atttp = AttributeInternalType.EntityNameReference;
                if ( CRMQueryTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EntityGenerator AddColumn changing type for Key: {0}, name: {0}",key, Name),CRMQueryTraceSwitch.Sw.Info);
            }
            switch (atttp)
            {
                case AttributeInternalType.Float:
                    AddFloat(sb,Name);
                    break;
                case AttributeInternalType.Integer:
                    AddNumber(sb,Name);
                    break;
                case AttributeInternalType.Status:
                    break;
                case AttributeInternalType.Picklist:
                    AddPickList(sb,Name);
                    break;
                case AttributeInternalType.Decimal:
                    AddDecimal(sb,Name);
                    break;
                case AttributeInternalType.Virtual:// virtual added by crm for exemple picklist option name or boolean->(iskitname)
                case AttributeInternalType.EntityNameReference:
                case AttributeInternalType.State:
                case AttributeInternalType.String:
                case AttributeInternalType.Memo:
                    AddString(sb,Name);
                    break;
                case AttributeInternalType.DateTime:
                    AddDateTime(sb,Name);
                    break;
                case AttributeInternalType.Money:
                    AddMoney(sb,Name);
                    break;
                case AttributeInternalType.Boolean:
                    AddBooleanProperty(sb,Name);
                    break;
                case AttributeInternalType.PrimaryKey:
                    break;
                case AttributeInternalType.UniqueIdentifier:
                    AddUniqueIdentifier(sb,Name);
                    break;
                case AttributeInternalType.Customer:
                    AddCustomer(sb,Name);
                    break;
                case AttributeInternalType.Owner:
                    AddOwner(sb,Name);
                    break;
                case AttributeInternalType.Lookup:
                    AddLookup(sb,Name);
                    break;
                default:
                    if ( CRMQueryTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EntityGenerator AddColumn missing type : {0}, name: {1}",attType, Name),CRMQueryTraceSwitch.Sw.Info);
                    break;
            }
        }

    }
}
